<?php

    /**
     * Class Navigate_model
     */
    class Authrule_model extends CI_Model
    {
        function __construct()
        {
            parent::__construct();
        }

        function saveRule( $data )
        {
            if ( !$data[ 'id' ] ) {
                $isok = $this->db->insert( 'auth_rule', $data );
            } else {
                $isok = $this->db->update( 'auth_rule', $data, [ 'id' => $data[ 'id' ] ] );
            }

            if ( !$isok ) Tool::exitError( "操作失败" );
            Tool::exitOk( "操作成功" );

        }

        function saveGroup( $data )
        {
            if ( !$data[ 'id' ] ) {
                $isok = $this->db->insert( 'auth_group', $data );
            } else {
                $isok = $this->db->update( 'auth_group', $data, [ 'id' => $data[ 'id' ] ] );
            }

            if ( !$isok ) Tool::exitError( "操作失败" );
            Tool::exitOk( "操作成功" );

        }

        function getGroupList()
        {
            return $this->db->get( 'auth_group' )->result_array();
        }
        function getGroupAcessList($uid)
        {

            $result=$this->db->query("select * from blog_auth_group LEFT JOIN (select * from blog_auth_group_access where uid=$uid) as tmp on blog_auth_group.id=tmp.group_id order by id")->result_array();
            return $result;
        }
        function saveGroupAcess($uid,array $group_ids)
        {
            $this->db->trans_start();
            $isok=$this->db->where('uid',$uid)->or_where('group_id',0)->delete('auth_group_access');
            $arr=[];
            foreach($group_ids as $k=>&$v)
            {
                $arr[$k]=['uid'=>$uid,'group_id'=>$v];
            }
            if($isok)
            {
                $isok=$this->db->insert_batch('auth_group_access', $arr);
            }
            $this->db->trans_complete();
            if(!$isok)Tool::exitError("出现错误",'saveGroupAcess');
            Tool::exitOk();

        }

    }

?>